import { createClient, createClientPool } from "redis"
import {logger} from '../utils/logger.js'
import type { RedisClientType, RedisClientPoolType } from "redis"

const {REDIS_HOST, REDIS_PORT, REDIS_USERNAME, REDIS_PASSWORD, REDIS_DATABASE} = process.env

export const redisDataSource: RedisClientPoolType = createClientPool({
	socket: {
		"host": REDIS_HOST,
		"port": Number(REDIS_PORT),
	},
	// "username": REDIS_USERNAME,
	"password": REDIS_PASSWORD,
	"database": Number(REDIS_DATABASE),
})

export function connectRedisDataSource(): Promise<RedisClientPoolType> {
	return redisDataSource
		.on("error", (error) => {
			logger.error(`Redis初始化异常: ${error}`)
		})
		.connect()
}

